/**
 * 
 */
package com.jdy.haoduoaiteacher.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.enums.SelectionMode;
import com.jdy.haoduoaiteacher.model.PainPointItem;
import com.jdy.haoduoaiteacher.util.HttpUtils;
import com.jdy.haoduoaiteacher.util.Tools;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * @author zp 2016-11-29 上午9:59:38
 */
public class PainPointCustomAdapter extends BaseAdapter {

  private final Context mContext;

  private final LayoutInflater mInflater;

  private final SparseArray<View> mSpArray;

  private  ArrayList<PainPointItem> mPainPointItemList;

  private OnPainPointSelectListener mOnPainPointSelectListener;
  private boolean isEditor = false;
  private ArrayList<Integer> mSelectedList;

  public SelectionMode SelectMode = SelectionMode.NONE;


  public void setSelectMode(SelectionMode sm) {
    SelectMode = sm;

    if (mPainPointItemList != null && !mPainPointItemList.isEmpty()) {
      if (SelectMode == SelectMode.SELECT_ALL) {
        for (PainPointItem item : mPainPointItemList) {
          item.isChecked = true;
        }
      } else if (SelectMode == SelectMode.DESELECT_ALL) {
        for (PainPointItem item : mPainPointItemList) {
          item.isChecked = false;
        }
      }
    }
  }

  public void setEditor(boolean iseditor) {
    this.isEditor = iseditor;
  }

  public PainPointCustomAdapter(Context ctx, ArrayList<PainPointItem> list) {
    this.mContext = ctx;
    this.mPainPointItemList = list;
    this.mInflater = LayoutInflater.from(mContext);
    this.mSpArray = new SparseArray<View>();
    this.mSelectedList = new ArrayList<Integer>();
  }

  public void setData(ArrayList<PainPointItem> list) {
    mPainPointItemList = list;
  }

  public void setSelectedList(ArrayList<Integer> list) {
    this.mSelectedList = list;
  }

  public void setPainPointSelectListener(OnPainPointSelectListener listener) {
    this.mOnPainPointSelectListener = listener;
  }


  public void remove(PainPointItem object) {
    mPainPointItemList.remove(object);
    notifyDataSetChanged();
  }

  public ArrayList<PainPointItem> getList() {
    return mPainPointItemList;
  }

  public void removeSelection() {
    for (PainPointItem item : mPainPointItemList) {
      item.isChecked = false;
    }

    notifyDataSetChanged();
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getCount()
   */
  @Override
  public int getCount() {
    if (mPainPointItemList != null)
      return mPainPointItemList.size();
    else
      return 0;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItem(int)
   */
  @Override
  public PainPointItem getItem(int position) {
    if (mPainPointItemList != null)
      return mPainPointItemList.get(position);
    else
      return null;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItemId(int)
   */
  @Override
  public long getItemId(int position) {
    if (mPainPointItemList != null)
      return position;
    else
      return 0;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getView(int, android.view.View,
   * android.view.ViewGroup)
   */
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
   final ViewHolder holder;
    // convertView = mSpArray.get(position);
    final PainPointItem ppi = mPainPointItemList.get(position);
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.custom_habit_item, null);
//      holder.touchAreaLayout = (RelativeLayout) convertView
//          .findViewById(R.id.touch_area_layout);
      holder.img = (ImageView) convertView
          .findViewById(R.id.img);
      holder.tv_clock = (TextView) convertView.findViewById(R.id.tv_clock);
      holder.tv_morning = (TextView) convertView.findViewById(R.id.tv_morningsong);
      holder.selectCb = (CheckBox) convertView.findViewById(R.id.select_cb);
      holder.radiobtn = (CheckBox) convertView.findViewById(R.id.radiobtn);
      holder.rela_parent = (RelativeLayout) convertView
              .findViewById(R.id.rela_parent);

      convertView.setTag(holder);
      // mSpArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    final ViewHolder holder2 = holder;

  //  holder.descriptionTv.setText(ppi.desc_short);
    holder.tv_morning.setText(ppi.name);


      if (Tools.isNotEmpty(ppi.img)) {
          if (ppi.img.contains("api.lexiaobao.net")) {
              Picasso.with(mContext).load(ppi.img).into(holder.img);
          } else {
              Picasso.with(mContext).load(HttpUtils.HOST_RESOURCE + ppi.img).into(holder.img);
          }
      }



    int lastIdx = ppi.time.lastIndexOf(":");
    holder.tv_clock.setText(ppi.time.substring(0, lastIdx));

    holder.selectCb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (mOnPainPointSelectListener != null) {
          mOnPainPointSelectListener.onPainPointSelect(ppi.id, isChecked);
        }

        if (isChecked) {
          ppi.enable = 1;
        } else {
          ppi.enable = 0;
        }
      }
    });

    holder.selectCb.setChecked(ppi.enable == 1);

//    holder.touchAreaLayout.setOnClickListener(new View.OnClickListener() {
//      @Override
//      public void onClick(View v) {
//
//        // if (!Tools.isFastDoubleClick()) {
//        Intent t = new Intent(mContext, PainPointDiscussionActivity.class);
//        t.putExtra("name", ppi.name);
//        t.putExtra("number", ppi.user_number);
//        t.putExtra("img", ppi.img);
//        t.putExtra("desc", ppi.desc);
//        t.putExtra("desc_short", ppi.desc_short);
//        t.putExtra("habit_id", ppi.id);
//        t.putExtra("selected", ppi.selected);
//        Bundle extraBundle = new Bundle();
//        extraBundle.putIntegerArrayList("selected_list", mSelectedList);
//        t.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//        t.putExtras(extraBundle);
//        ((Activity) mContext).startActivityForResult(t, 111);
//        // }
//
//      }
//    });

    if (isEditor) { // 处于编辑形态
      holder.radiobtn.setVisibility(View.VISIBLE);
      holder.selectCb.setVisibility(View.GONE);
    } else {
      holder.radiobtn.setVisibility(View.GONE);
      holder.selectCb.setVisibility(View.VISIBLE);
    }

    if (isEditor) {
      holder.rela_parent.setClickable(true);
      holder.rela_parent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          holder.radiobtn.setChecked(!holder.radiobtn.isChecked());
          mPainPointItemList.get(position).isChecked = holder.radiobtn.isChecked();
        }
      });
    } else {
      holder.rela_parent.setClickable(false);
    }

    holder.radiobtn.setChecked(mPainPointItemList.get(position).isChecked);

    return convertView;
  }

  private static class ViewHolder {
    ImageView img;
    TextView tv_clock;
    TextView tv_morning;
    CheckBox selectCb;
    private CheckBox radiobtn;
    private RelativeLayout rela_parent;
    View slayout;
  }

}
